﻿namespace S7.Net.Types
{
    using System;

    public static class Counter
    {
        public static ushort FromByteArray(byte[] bytes)
        {
            if (bytes.Length != 2)
            {
                throw new ArgumentException("Wrong number of bytes. Bytes array must contain 2 bytes.");
            }
            return (ushort) ((bytes[0] << 8) | bytes[1]);
        }

        public static ushort[] ToArray(byte[] bytes)
        {
            ushort[] numArray = new ushort[bytes.Length / 2];
            int num = 0;
            for (int i = 0; i < (bytes.Length / 2); i++)
            {
                byte[] buffer1 = new byte[] { bytes[num++], bytes[num++] };
                numArray[i] = FromByteArray(buffer1);
            }
            return numArray;
        }

        public static byte[] ToByteArray(ushort[] value)
        {
            ByteArray array = new ByteArray();
            foreach (ushort num2 in value)
            {
                array.Add(ToByteArray(num2));
            }
            return array.array;
        }

        public static byte[] ToByteArray(ushort value)
        {
            return new byte[] { ((byte) ((value << 8) & 0xff)), ((byte) (value & 0xff)) };
        }
    }
}

